%GLOBAL CWD; %LET CWD = %SUBSTR(%SYSGET(SAS_EXECFILEPATH), 1, %EVAL(%LENGTH(%SYSGET(SAS_EXECFILEPATH)) - %LENGTH(%SYSGET(SAS_EXECFILENAME)) - 1));
%INCLUDE "&CWD\TOOLS.SAS";

/* SET ELI & CLUSTER */
%LET ELI = FN031;
%LET CLUSTER = 01A;

/* IDENTIFYING HANDLE */
%LET COM = &ELI._&CLUSTER.;

/* ITEM-SPECIFIC RIGHT HAND SIDE VARIABLES */
%LET RHS= 
    B2 C1-C3 E1-E16 E98SP F2 G2-G6
    H1-H2 H4-H9 I1-I6 J1-J4 K97SP K98SP BRAND1-BRAND9 L2-L4 CALCIUM VITC LOSALT R2
    MNAMT MNAMTSQ
    ;

%LET STYR=0305;
%LET FIRSTPER=20&STYR;
%LET LASTPER = 200709;
%LET STDUM=D&STYR;
%LET RHYRS= D0306--D0709;

%LET NSPER=53;
%LET NVEC=118;

/* DEFINE ITEM-SPECIFIC PRICE EDITS */
%MACRO EDITS;
    IF A="A1";
    IF CLUSTER="01A" AND PERIOD GT 200304;

    IF .01 < PRICE < .25;
    *;
    IF LNAMT LE 5.63;
    IF D="D6" OR D="D99" THEN DELETE;
    IF E="" OR H="" OR I="" THEN DELETE;
%MEND;

/* DEFINE ITEM-SPECIFIC DUMMIES */
%MACRO VARS;
    J1=(J="J1"); J2=(J="J2"); J3=(J="J3"); J4=(J="J4"); 
    ARRAY JARRAY (4) J1-J4;
    DO COUNT=1 TO 4;
        IF J="" THEN JARRAY(COUNT)=.;
    END;
    K97SP=(K="K97"); K98SP=(K="K98"); 
    L1=(L="L1"); L2=(L="L2"); L3=(L="L3"); L4=(L="L4"); 
    ARRAY LARRAY (4) L1-L4;
    DO COUNT=1 TO 4;
        IF L="" THEN LARRAY(COUNT)=.;
    END;
    
    BSTRING=K97; 
    IF CLUSTER="01A" THEN DO;
        IF BSTRING="" THEN BSTRING=K98;
        IF BSTRING="" THEN BSTRING=K99;
    END;
    IF SUBSTR(BSTRING,1,7)="**********" OR SUBSTR(BSTRING,1,7)="**********" OR INDEX(BSTRING,'**********') 
       OR INDEX(BSTRING,'**********') OR SUBSTR(BSTRING,1,3)="**********" THEN BRAND1=1; ELSE BRAND1=0;
    IF SUBSTR(BSTRING,1,4)="**********" THEN BRAND2=1; ELSE BRAND2=0;
    IF SUBSTR(BSTRING,1,5)="**********" OR INDEX(BSTRING,'**********') OR INDEX(BSTRING,'**********')
       OR INDEX(BSTRING,'**********') OR INDEX(BSTRING,'**********') THEN BRAND3=1; ELSE BRAND3=0;
    IF SUBSTR(BSTRING,1,7)="**********" OR SUBSTR(BSTRING,1,8)="**********" 
       OR SUBSTR(BSTRING,1,7)="**********" THEN BRAND4=1; ELSE BRAND4=0;
    IF SUBSTR(BSTRING,1,4)="**********" THEN BRAND5=1; ELSE BRAND5=0;
    IF INDEX(BSTRING,'**********') OR INDEX(BSTRING,'**********') THEN BRAND6=1; ELSE BRAND6=0;
    IF INDEX(BSTRING,'**********') OR INDEX(BSTRING,'**********') THEN BRAND7=1; ELSE BRAND7=0;
    IF SUBSTR(BSTRING,1,9)="**********" OR INDEX(BSTRING,'**********') THEN BRAND8=1; ELSE BRAND8=0;
    IF SUBSTR(BSTRING,1,5)="**********" THEN BRAND9=1; ELSE BRAND9=0;
    ARRAY BRARRAY (9) BRAND1-BRAND9;
    DO COUNT=1 TO 9;
        IF G="" THEN BRARRAY(COUNT)=.;
    END;
    
    IF E="E1" THEN ORANGE=1; ELSE ORANGE=0;
    IF (CLUSTER="01" AND E="E2") OR (CLUSTER="01A" AND E IN ("E5","E6")) THEN GRAPE=1; ELSE GRAPE=0;
    IF (CLUSTER="01" AND E="E3") OR (CLUSTER="01A" AND E="E7") THEN CRANBERRY=1; ELSE CRANBERRY=0;
    IF (CLUSTER="01" AND E="E4") OR (CLUSTER="01A" AND E IN ("E3","E4")) THEN APPLE=1; ELSE APPLE=0;
    IF (CLUSTER="01" AND E="E7") OR (CLUSTER="01A" AND E="E2")  THEN GRAPEFRUIT=1; ELSE GRAPEFRUIT=0;
    IF (CLUSTER="01" AND E="E8") OR (CLUSTER="01A" AND E="E9")  THEN PINEAPPLE=1; ELSE PINEAPPLE=0;
    IF (CLUSTER="01" AND E="E16") OR (CLUSTER="01A" AND E="E15")  THEN TOMATO=1; ELSE TOMATO=0;
    IF (CLUSTER="01" AND E="E18") OR (CLUSTER="01A" AND E="E16")  THEN VEGETABLE=1; ELSE VEGETABLE=0;
    IF (CLUSTER="01" AND E="E98") OR (CLUSTER="01A" AND E="E98")  THEN COMBO=1; ELSE COMBO=0;
    
    IF (CLUSTER="01" AND (INDEX(L,'CALCIUM') OR INDEX(M,'CALCIUM'))) OR (CLUSTER="01A" AND M="M1")  THEN CALCIUM=1; ELSE CALCIUM=0;
    IF (CLUSTER="01" AND (SUBSTR(L,1,3)="VIT" OR SUBSTR(M,1,3)="VIT")) OR (CLUSTER="01A" AND N="N1")  THEN VITC=1; ELSE VITC=0;
    IF (CLUSTER="01" AND G="G2") OR (CLUSTER="01A" AND P="P1")  THEN LOSALT=1; ELSE LOSALT=0;
    
    I1=(I="I1"); I2=(I="I2"); I3=(I="I3"); I4=(I="I4"); 
    I5=(I="I5"); I6=(I="I6"); 
    E9=(E="E9"); E10=(E="E10"); E11=(E="E11"); E12=(E="E12"); 
    E13=(E="E13"); E14=(E="E14"); E15=(E="E15"); E16=(E="E16"); 
    E98SP=(E="E98"); 
    R2=(R="R2");
    
    ARRAY SPECARRAY (*) B2 C2 C3 C99SP ORANGE--COMBO F1 G2-G6 J1-J4 L2-L4 BRAND1-BRAND9
        K97SP K98SP CALCIUM--LOSALT DISCDEP--OTHSTORE;
    DO COUNT=1 TO DIM(SPECARRAY);
        IF SPECARRAY(COUNT)=. THEN DELETE;
    END;
%MEND;

LIBNAME IN "&CWD\DATA";
LIBNAME OUT "&CWD\DATA";

FILENAME OUTLOGS "&CWD\OUTPUT\LOGINDEXES.XLS";
FILENAME OUTCONTS "&CWD\OUTPUT\LOGCONTRIBS.XLS";
FILENAME OUTDISCS "&CWD\OUTPUT\DISCOUNTS.XLS";
FILENAME OUTPCTLS "&CWD\OUTPUT\PCTLS.XLS";

%INCLUDE "&CWD\TABLESTOBUILD.SAS";
